<template>
  <div>
    <el-alert title="查询信息" type="info" :closable="false"></el-alert>
    <el-form inline :model="form">
      <el-form-item label="角色名称">
        <el-input
          v-model="form.roleName"
          placeholder="请输入角色名称"
          size="medium"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" size="medium" @click="search(1)">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" size="medium" @click="add">新增</el-button>
      </el-form-item>
    </el-form>
    <el-alert title="角色信息列表" type="info" :closable="false"></el-alert>
    <el-table :data="tableData" stripe v-loading="loading">
      <el-table-column
        label="角色名称"
        prop="roleName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="角色描述"
        prop="remark"
        align="center"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-link type="primary" :underline="false" @click="modify(scope.row)"
            >修改</el-link
          >&nbsp;
          <el-link type="primary" :underline="false" @click="del(scope.row)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <el-col :span="10">
        <el-pagination
          background
          :current-page="pageData.currentPage"
          layout="total, prev, pager, next, jumper"
          :total="pageData.count"
          :page-size="pageData.pageSize"
          @current-change="handleCurrentChange"
          v-if="pageData.count && pageData.count != 0"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="增加角色" :visible.sync="addRole" width="25%">
      <el-form
        :model="addForm"
        ref="addForm"
        :rules="rules"
        size="medium"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            v-model="addForm.roleName"
            placeholder="请输入角色名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="remark">
          <el-input
            v-model="addForm.remark"
            placeholder="请输入角色描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addRole = false">取 消</el-button>
        <el-button type="primary" @click="handle">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改角色" :visible.sync="modifyRole" width="25%">
      <el-form
        :model="modifyForm"
        ref="modifyForm"
        :rules="rules"
        size="medium"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            v-model="modifyForm.roleName"
            placeholder="请输入角色名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="remark">
          <el-input
            v-model="modifyForm.remark"
            placeholder="请输入角色描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modifyRole = false">取 消</el-button>
        <el-button type="primary" @click="handle2">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Axios from "axios";
import moment from "moment";
import * as serviceApi from "../../common/service.config";
Axios.defaults.withcredentials = true;

export default {
  name: "RoleManagement",
  data() {
    return {
      form: {
        roleName: "",
      },
      tableData: null,
      pageData: {
        currentPage: null,
        count: null,
        pageSize: null,
      },
      addRole: false,
      addForm: {
        roleName: "",
        remark: "",
      },
      modifyRole: false,
      modifyForm: {
        roleName: "",
        remark: "",
      },
      rules: {
        roleName: [
          {
            pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
            message: "不支持特殊字符！",
            trigger: "blur",
          },
        ],
        remark: [
          {
            pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
            message: "不支持特殊字符！",
            trigger: "blur",
          },
        ],
      },
      loading: null,
    };
  },
  methods: {
    queryRoleList(queryData) {
      this.loading = true;
      Axios({
        url: serviceApi.KB_API,
        method: "post",
        data: `appMsg={"parameter":"{\\"page\\":{\\"currentPage\\":${queryData.currentPage},\\"pageSize\\":10},\\"roleName\\":\\"${queryData.roleName}\\",\\"managerId\\":\\"\\"}","serviceCode":"listRoleInfo","timestamp":"","uuid":""}`,
      }).then((res) => {
        this.loading = false;
        this.pageData = res.data.parameter.page;
        let list = res.data.parameter.list;
        for (let i = 0; i < list.length; i++) {
          list[i].createTime = moment(list[i].createTime).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        this.tableData = list;
      });
    },
    search(currentPage) {
      let queryData = {
        currentPage: currentPage,
        roleName: this.form.roleName,
      };
      this.queryRoleList(queryData);
    },
    handleCurrentChange(val) {
      this.search(val);
    },
    add() {
      this.addRole = true;
      this.addForm = {
        roleName: "",
        remark: "",
      };
    },
    handle() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          this.addRole = false;
          Axios({
            url: serviceApi.KB_API,
            method: "post",
            data: `appMsg={"parameter":"{\\"roleName\\":\\"${this.addForm.roleName}\\",\\"remark\\":\\"${this.addForm.remark}\\"}","serviceCode":"addRoleInfo","timestamp":"","uuid":""}`,
          }).then((res) => {
            let status = res.data.parameter.status;
            let message = res.data.parameter.message;
            switch (status) {
              case "0000":
                this.$message({
                  message: message,
                  type: "success",
                  duration: 1000,
                  onClose: () => {
                    let queryData = {
                      currentPage: "1",
                      roleName: "",
                    };
                    this.queryRoleList(queryData);
                  },
                });
                break;
              case "99":
                this.$message({
                  message: message,
                  type: "warning",
                });
            }
          });
        }
      });
    },
    modify(row) {
      this.modifyRole = true;
      this.modifyForm = Object.assign({}, row);
    },
    handle2() {
      this.$refs["modifyForm"].validate((valid) => {
        if (valid) {
          this.modifyRole = false;
          Axios({
            url: serviceApi.KB_API,
            method: "post",
            data: `appMsg={"parameter":"{\\"id\\":\\"${this.modifyForm.id}\\",\\"roleName\\":\\"${this.modifyForm.roleName}\\",\\"remark\\":\\"${this.modifyForm.remark}\\"}","serviceCode":"updateRoleInfo","timestamp":"","uuid":""}`,
          }).then((res) => {
            let status = res.data.parameter.status;
            let message = res.data.parameter.message;
            switch (status) {
              case "0000":
                this.$message({
                  message: message,
                  type: "success",
                  duration: 1000,
                  onClose: () => {
                    let queryData = {
                      currentPage: "1",
                      roleName: "",
                    };
                    this.queryRoleList(queryData);
                  },
                });
                break;
              case "99":
                this.$message({
                  message: message,
                  type: "warning",
                });
            }
          });
        }
      });
    },
    del(row) {
      this.$confirm("您确定要删除该条数据吗？", "删除角色", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          Axios({
            url: serviceApi.KB_API,
            method: "post",
            data: `appMsg={"parameter":"{\\"roleId\\":\\"${row.id}\\"}","serviceCode":"deleteRoleInfo","timestamp":"","uuid":""}`,
          }).then((res) => {
            let message = res.data.parameter.message;
            this.$message({
              type: "success",
              message: message,
              duration: 1000,
              onClose: () => {
                let queryData = {
                  currentPage: "1",
                  roleName: "",
                };
                this.queryRoleList(queryData);
              },
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消删除!",
          });
        });
    },
  },
  created() {
    let queryData = {
      roleName: "",
      currentPage: "1",
    };
    this.queryRoleList(queryData);
  },
};
</script>

<style lang="less" scoped>
.el-alert {
  line-height: 30px;
  margin-top: 20px;
  /deep/ .el-alert__title {
    font-size: 16px;
  }
}
.el-form {
  margin: 20px 0;
  margin-left: 30px;
}
</style>